Repository navigation
fix(core): correctly discover value providers in discovery service - #17618
Conversation
Providers registered via useValue with a custom discovery decorator were not being discovered by DiscoveryService.getProviders because a ternary condition in DiscoverableMetaHostCollection was missing a negation operator. Fixes nestjs#17617
|
I ran into the same issue and arrived at the same change, so I'll leave this one to you — yours covers more ground with the The comment and the typo landed together. Both come from The spec you changed is a characterization test. It was added in On the performance note, which is what I'd expect to come up: it says
The old condition was truthy for every class provider, which is the dominant case in an app; the new one is truthy only for Happy to help verify if that's useful. |
|
Thanks @johnnyhuirilef — really appreciate the extra context, especially the git archaeology on the comment/typo landing together in the same commit. That performance breakdown is a great point to have on record; happy for maintainers to reference it during review if the deferred-instance-access concern comes up. |
|
lgtm |
Fixes #17617
What was wrong
DiscoveryService.getProviders({ metadataKey })failed to return providers registered viauseValuewhen the target class was decorated with a custom decorator fromDiscoveryService.createDecorator(). OnlyuseClass-style providers were discovered.Why it happened
In
DiscoverableMetaHostCollection.getMetaKeyByInstanceWrapper, the ternary condition was missing a negation that the accompanying code comment explicitly describes:For
useValueproviders,metatypeisnullandinjectisundefined, so the condition evaluated falsy and never fell back toinstance.constructor, leaving the lookup keyed onnull.What was changed
Added the missing negation:
This restores the fallback to
instance.constructorforuseValueproviders while leaving theuseClass/useFactorybehavior unchanged.How it was tested
discoverable-meta-host-collection.spec.tsthat previously asserted the incorrect behavior — it now asserts the provider is correctly discovered.discovery-service.spec.tscoveringDiscoveryService.getProviders({ metadataKey })with auseValueprovider.packages/core/test/discovery/) — all 45 tests pass.Why the change is safe
The fix only affects the branch previously reached when
metatypeis falsy (i.e.,useValueproviders with noinject). ExistinguseClassanduseFactorycode paths are unaffected, sincemetatype/injectremain truthy for those cases and the same branch is taken as before.Performance impact
The old condition read
wrapper.instance(the getter the comment says to defer) for everyuseClassprovider — the dominant case in most apps. The new condition only reads it foruseValueanduseFactoryproviders:.instance.instanceuseClassuseFactoryuseValueSo this change reduces reads of that getter overall rather than adding to them — it doesn't work against the deferred-access optimization the comment describes.
(Credit to @johnnyhuirilef for working this out and confirming independently.)